import logging
logger = logging.getLogger('zhipu_web_search_sse')
from fastmcp import Client
from mcp_server import mcp
from config.mcp_config.mcp.mcp import zhipu_web_search_sse


# 初始化客户端
client = Client(zhipu_web_search_sse)

@mcp.tool()
async def zhipu_web_search_sse_tool(query: str):
    """
    智谱联网搜索工具,如果用户需要一些实时性的信息,请使用这个工具,比如:
    1. 实时新闻
    2. 最新教程
    3. 最近发生的事
    4. 明显八卦



    """
    try:
        async with client:
            # 调用远程工具
            response = await client.call_tool(
                "webSearchStd",
                {
                    "search_query": query
                }
            )
            result = response.content[0].text
            logger.info(f"查询结果: {result}")

            return result
    except Exception as e:
        error_msg = f"调用失败：{str(e)}"
        logger.error(error_msg)
        return error_msg
    finally:
        # 关闭客户端
        client.close()
